use std::fs;
use std::collections::HashMap;

pub fn count_provinces() -> String {
    let data = fs::read_to_string("district.json").unwrap();

    let mut hash:HashMap<String, Vec<String>> = HashMap::new();
    let mut find_digit = false;
    let mut start = false;
    let mut is_key = false;
    let mut is_val = false;
    let mut current_key = "".to_string();
    let mut key = vec![];
    let mut val = vec![];
    let mut ret = vec![];
    let chs = data.chars().filter(|ch| !ch.is_ascii_control() && !ch.is_ascii_whitespace() ).collect::<Vec<_>>();
    for ch in chs {
        match ch {
            '{' => {
                if find_digit {
                    start = true;
                    is_key = true;
                }
            },
            '}' => {
                if start {
                    let mut city_group = HashMap::new();
                    for key in hash.keys() {
                        if city_group.is_empty() {
                            city_group.insert(key.clone(), hash.get(key).unwrap().clone());
                        }else{
                            let mut flag = true;
                            let mut city_group_clone = city_group.clone();
                            for (k,v) in &city_group {
                                let val = hash.get(key).unwrap().clone();
                                let contain = v.iter().find(|x| val.iter().find(|v| x==v).is_some()).is_some();

                                if contain || v.contains(key) || hash.get(key).unwrap().contains(k) {
                                    flag = false;
                                    let mut new_val = v.clone();
                                    new_val.push(key.clone());
                                    new_val.append(&mut hash.get(key).unwrap().clone());
                                    city_group_clone.insert(k.clone(), new_val);
                                }
                            }
                            if flag {
                                city_group.insert(key.clone(), hash.get(key).unwrap().clone());
                            }else {
                                city_group = city_group_clone;
                            }
                        }
                    }
                    ret.push(city_group.keys().len().to_string());
                    hash = HashMap::new();
                    start = false;
                }
            },
            '[' => {
                if is_key {
                    is_key = false;
                    is_val = true;
                    current_key = key.join("");
                    key = vec![];
                    let mut contain = false;
                    for key in hash.keys() {
                        let val = hash.get(key).unwrap().clone();
                        contain = val.iter().find(|v| current_key==**v).is_some();
                        if contain {
                            current_key = key.clone();
                            break;
                        }
                    }
                    if let Some(_k) = hash.get(&current_key) {
                       
                    }else if !contain{
                        hash.entry(current_key.clone()).or_insert(vec![]);
                    }
                }
            },
            ']' => {
                if let Some(k) = hash.get(&current_key) {
                    let mut new_val = k.clone();
                    new_val.push(val.join(""));
                    hash.insert(current_key.clone(), new_val);
                    val = vec![];
                }
                is_val = false;
            },
            ':' => {},
            ',' => {
                if !is_val {
                    is_key = true;
                }else if let Some(k) = hash.get(&current_key) {
                    let mut new_val = k.clone();
                    new_val.push(val.join(""));
                    hash.insert(current_key.clone(), new_val);
                    val = vec![];
                }
            },
            '"' => {},
            _ => {
                if ch.is_digit(10) {
                    find_digit = true;
                }else if is_key {
                    key.push(ch.to_string());
                }else if is_val {
                    val.push(ch.to_string());
                }
            }
        };
    }
    // iter().map(|n| n.to_string()).collect::<Vec<_>>().
    ret.join(",")
}
